package com.zt.ztyh.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zt.ztyh.admin.service.LoanService;
import com.zt.ztyh.admin.service.impl.LoanServiceImpl;
import com.zt.ztyh.model.common.Result;
import com.zt.ztyh.model.common.ResultCodeEnum;
import com.zt.ztyh.model.dto.LoanDto;
import com.zt.ztyh.model.entity.Loan;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ztyh/admin")
@Tag(name = "贷款管理")
public class LoanAdminController {

    @Autowired
    private LoanService loanService;
    @Autowired
    private LoanServiceImpl loanServiceImpl;

    @GetMapping("/{page}/{limit}")
    @Operation(summary = "管理贷款分页查询")
    public Result<Page<Loan>> findAllLoan(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit,
                                          LoanDto loanDto) {
            loanServiceImpl.beOverdue();
        return Result.build(loanService.findAllLoan(page, limit,loanDto), ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/saveLoan")
    @Operation(summary = "新增和修改贷款")
    public Result saveLoan(@RequestBody Loan loan) {
        System.out.println("loan="+loan);
        loan.setCreatetime(new Date());
        loanService.saveOrUpdate(loan);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping("/{lid}")
    @Operation(summary = "删除贷款")
    public Result deleteLoan(@PathVariable("lid") Long lid) {
        loanService.removeById(lid);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/updateState/{lid}")
    @Operation(summary = "修改贷款状态")
    public Result updateState(@PathVariable("lid") Long lid) {
        loanService.updateState(lid);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}
